package IT.DataAccess;

import IT.Entities.UsuarioDetalle;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DatUsuarioDetalle implements IDatUsuarioDetalle{
    private List<UsuarioDetalle> oLista;
    private ResultSet oRst;
    private Connection oConexion;
    private CallableStatement oCst;    
    private BDConexion oBase;
    
    public List<UsuarioDetalle> Listar(int nUsuario) {      
        oLista=new ArrayList<UsuarioDetalle>();
         
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_UsuarioDetalleListar(?)}");
            oCst.setInt(1, nUsuario);
            oRst = oCst.executeQuery();

            while (oRst.next()) {
                oLista.add(new UsuarioDetalle(
                        oRst.getInt("cod_usu_in"),                         
                        oRst.getInt("cod_per_in")));
            }
            oRst.close();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
         return oLista;
    }
    
    public void Insertar(UsuarioDetalle ent) {
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_UsuarioDetalleInsertar(?,?)}");              
            oCst.setInt(1, ent.getCod_usu_in());
            oCst.setInt(2, ent.getCod_per_in());      
            oCst.execute();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Eliminar(UsuarioDetalle ent) {
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_UsuarioDetalleEliminar(?,?)}");              
            oCst.setInt(1, ent.getCod_usu_in());
            oCst.setInt(2, ent.getCod_per_in());      
            oCst.execute();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void EliminarTodo(int nUsuario) {
         try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_UsuarioDetalleEliminarTodo(?)}");              
            oCst.setInt(1,nUsuario);               
            oCst.execute();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
